﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Windows.UI.Xaml
{
    public static class ApplicationExtention
    {
        /// <summary>
        /// 获取APP的资源文件
        /// </summary>
        /// <param name="app"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public static async Task<Windows.Storage.StorageFile> GetApplicationStream(this Application app, string path)
        {
            string id = Windows.ApplicationModel.Package.Current.Id.Name;
            string searchPath = "/Files/" + path;
            foreach (var item in Windows.ApplicationModel.Resources.Core.ResourceManager.Current.AllResourceMaps)
            {
                if (item.Key == id)
                {
                    foreach (var i in item.Value.Values)
                    {
                        if (i.Uri.LocalPath == searchPath)
                        {
                            var file = await i.Resolve().GetValueAsFileAsync();
                            return file;
                            //return await aa.OpenAsync(Storage.FileAccessMode.Read);
                            //var buffer = await Storage.FileIO.ReadBufferAsync(aa);
                            //return buffer;
                        }
                    }
                }
            }
            //找不到，那么返回null
            return null;

        }
    }
}
